This section visualizes the geographic distribution of collisions across New York City.
set.seed(123)
mvc_sample <- mvc_clean |>
filter(!is.na(latitude), !is.na(longitude)) |>
slice_sample(n = 5000)
leaflet(mvc_sample) |>
addProviderTiles(providers$CartoDB.Positron) |>
setView(lng = -73.97, lat = 40.75, zoom = 11) |>
addCircleMarkers(
lng = ~longitude,
lat = ~latitude,
radius = 3,
stroke = FALSE,
fillOpacity = 0.5,
color = "#2C93E8"
)mvc_sample2 <- mvc_clean |>
mutate(
severity = case_when(
number_of_persons_killed > 0 ~ "Fatal",
number_of_persons_injured > 0 ~ "Injury",
TRUE ~ "Property Damage Only"
)
) |>
slice_sample(n = 5000)
pal <- colorFactor(
palette = c("#7db7ff", "#2C93E8", "#FF5733"),
domain = c("Property Damage Only", "Injury", "Fatal")
)
leaflet(mvc_sample2) |>
addProviderTiles(providers$CartoDB.Positron) |>
setView(lng = -73.97, lat = 40.75, zoom = 11) |>
addCircleMarkers(
lng = ~longitude,
lat = ~latitude,
radius = 3,
stroke = FALSE,
fillOpacity = 0.6,
color = ~pal(severity),
label = ~htmlEscape(severity)
) |>
addLegend(
position = "bottomright",
pal = pal,
values = mvc_sample2$severity,
title = "Severity"
)